package person.bing.fbcloud.leetcode;

/**
 * @author: mengfanbing
 * @date: 2020/8/3 9:40
 */
public class Q415 {
    public static void main(String[] args) {
        String num1 = "0";
        String num2 = "0";
        System.out.println(addStrings(num1,num2));
    }

    public static String addStrings(String num1, String num2) {
        StringBuilder sb = new StringBuilder();
        int oneLength = num1.length();
        int twoLength = num2.length();
        int maxLength = oneLength > twoLength ? oneLength : twoLength;
        int plus = 0;
        for (int i=0;i<=maxLength;i++){
            int one = 0;
            int two = 0;
            if (oneLength > 0){
                one = num1.charAt(oneLength-1) - '0';
                oneLength--;
            }
            if (twoLength > 0){
                two = num2.charAt(twoLength-1) - '0';
                twoLength--;
            }
            int sum = one + two + plus;
            if (sum >= 10){
                plus = 1;
                sum = sum % 10;
            }else {
                plus = 0;
            }
            if (i == maxLength && sum > 0){
                sb.append(sum);
            }else if(i < maxLength){
                sb.append(sum);
            }
        }
        return sb.reverse().toString();
    }
}
